package com.ys.controller.api.sys;

import com.ys.entity.finance.TbCompanyBankInfoAccount;
import com.ys.entity.sys.TbCompanyBankInfo;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.TbCompanyBankInfoService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 公司银行账户管理
 */
@Controller
@RequestMapping("/api/inter/tbCompanyBankInfo")
public class ApiInterTbCompanyBankInfoController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTbCompanyBankInfoController.class);

    @Autowired
    private CommonApiService<TbCompanyBankInfo> commonApiService;

    @Autowired
    private TbCompanyBankInfoService tbCompanyBankInfoService;

    @RequiresPermissions("tbCompanyBankInfo:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TbCompanyBankInfo cond){
        cond.addLeftJoin("TS_UserInfo B on  B.UserId = A.UserId")
        .addLeftJoin("TP_AmazonShopExchangeRate C on  C.id = A.rateId")
        .addField("B.userName,B.roleName,C.code rateCode")
        .addLeftJoin("TB_CompanyInfo ci ON ci.Vid = A.CompanyInfoVid")//所属公司
        .addField("ci.Vname AS companyInfoName")//
        .setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }
    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("公司银行信息新增传参：" + params);
            //修改
            return tbCompanyBankInfoService.add(params);
        } catch (Exception e) {
            logger.error("公司银行信息新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("公司银行信息修改传参：" + params);
            //修改
            return tbCompanyBankInfoService.edit(params);
        } catch (Exception e) {
            logger.error("公司银行信息修改出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    @RequestMapping("/searchFk")
    @ResponseBody
    public LayuiTableVoJA searchFk(@ModelAttribute TbCompanyBankInfo cond){
        cond.addLeftJoin("TS_UserInfo B on  B.UserId = A.UserId")
                .addLeftJoin("TP_AmazonShopExchangeRate C on  C.id = A.rateId")
                .addLeftJoin("TB_CompanyInfo D on  D.Vid = A.CompanyInfoVid")
                .addField("B.userName,B.roleName,C.code rateCode,D.Vname orgName")
                .where("A.status = 1")
                .setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }

    /*
     * 给应付模块调用
     */
    @RequestMapping("/searchForNeedPay")
    @ResponseBody
    public LayuiTableVoJA searchForNeedPay(@ModelAttribute TbCompanyBankInfo cond){
    	
        cond
            .addLeftJoin("TB_CompanyInfo B on  B.Vid = A.CompanyInfoVid")
            .addField("B.Vname companyVname");
        return commonApiService.searchLay(cond);
    }


    /*
     * 采购合同单调用
     */
    @RequestMapping("/searchForModalByContract")
    @ResponseBody
    public LayuiTableVoJA searchForModalByContract(@ModelAttribute TbCompanyBankInfo cond, String conditions){
        cond
                .addLeftJoin("TB_CompanyInfo B on  B.Vid = A.CompanyInfoVid")
                .addField("B.Vname companyVname")
                .addField("B.Tax tax")
        ;
        return commonApiService.searchLay(cond);
    }


    /*
     * 采购付款单调用
     */
    @RequestMapping("/tbCompanyBankInfoCg")
    @ResponseBody
    public LayuiTableVoJA tbCompanyBankInfoCg(@ModelAttribute TbCompanyBankInfo cond, String conditions){
        cond.addLeftJoin("TS_UserInfo B on  B.UserId = A.UserId")
                .addLeftJoin("TP_AmazonShopExchangeRate C on  C.id = A.rateId")
                .addField("B.userName,B.roleName,C.code rateCode")
                .addLeftJoin("TB_CompanyInfo ci ON ci.Vid = A.CompanyInfoVid")//所属公司
                .addField("ci.Vname AS companyInfoName")//
                .addField("CASE WHEN A.natureAccount = 0 THEN '银行对公账户' WHEN A.natureAccount = 1 THEN '支付宝账户' ELSE '第三方平台账户' END natureAccountName")//账户性质

                .setStatus(1)
                .setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }

    @RequestMapping("/tbCompanyBankInfo_ZJZZ")
    @ResponseBody
    public LayuiTableVoJA tbCompanyBankInfo_ZJZZ(@ModelAttribute TbCompanyBankInfo cond){
        cond.addLeftJoin("TS_UserInfo B on  B.UserId = A.UserId")
                .addLeftJoin("TP_AmazonShopExchangeRate C on  C.id = A.rateId")
                .addField("B.userName,B.roleName,C.code rateCode")
                .addLeftJoin("TB_CompanyInfo ci ON ci.Vid = A.CompanyInfoVid")//所属公司
                .addField("ci.Vname AS companyInfoName")//
                .where("A.status = 1")
                .setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }
}
